package com.decoration.basic.service.impl;

import com.decoration.basic.entity.DecCtctionOutMsg;
import com.decoration.basic.entity.DecCtctionPo;
import com.decoration.basic.entity.FebackPo;
import com.decoration.basic.entity.JsonResult;
import com.decoration.basic.mapper.DecCtctionMapper;
import com.decoration.basic.mapper.DecFeedbackMapper;
import com.decoration.basic.service.DecCtctionService;
import com.decoration.basic.service.DecFeedbackService;
import com.decoration.basic.util.DateFormatUtils;
import com.decoration.basic.util.SnowFlake;
import com.decoration.basic.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DecFeedbackServiceImpl implements DecFeedbackService {

    @Resource
    DecFeedbackMapper decFeedbackMapper;
    @Resource
    DecCtctionMapper decCtctionMapper;

    @Override
    public JsonResult update(FebackPo inpo) {
        JsonResult result = new JsonResult();
        FebackPo outlist = null;
        try {
            outlist = decFeedbackMapper.searchFedbackPo(inpo);
            if (!outlist.getUserId().equals(inpo.getUserId())){
                result.setCode(JsonResult.FAILED_CODE);
                result.setMsg("反馈信息只能本人修改！");
                return result;
            }
            if (StringUtil.isNotEmpty(inpo.getFebackDesc())){
                outlist.setFebackDesc(inpo.getFebackDesc());
            }
            if (inpo.getFebackPiclist() != null && inpo.getFebackPiclist().size() >0) {
                String piclist = StringUtil.arrayAsStr(inpo.getFebackPiclist());
                outlist.setPicStrList(piclist);
            }
            outlist.setFebackDt(DateFormatUtils.getNowDay("yyyy-MM-dd"));
            decFeedbackMapper.update(outlist);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(JsonResult.FAILED_CODE);
            result.setMsg("更新反馈信息失败！");
            return result;
        }
        result.setCode(JsonResult.SUCCESS_CODE);
        result.setMsg("更新反馈信息成功！");
        return result;
    }

    @Override
    public JsonResult insert(FebackPo inpo) {
        JsonResult result = new JsonResult();
        try {
            DecCtctionPo ctction = null;
            try {
                ctction =decCtctionMapper.findByctcid(inpo.getCtcid());
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (ctction != null){
                inpo.setCtcName(ctction.getCtcName());
            }
            inpo.setFebackid(String.valueOf(SnowFlake.getInstance().nextId()));
            String piclist = StringUtil.arrayAsStr(inpo.getFebackPiclist());
            inpo.setPicStrList(piclist);
            inpo.setFebackDt(DateFormatUtils.getNowDay("yyyy-MM-dd"));
            decFeedbackMapper.insert(inpo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(JsonResult.FAILED_CODE);
            result.setMsg("新增反馈信息失败！");
            return result;
        }
        result.setCode(JsonResult.SUCCESS_CODE);
        result.setMsg("新增反馈信息成功！");
        return result;
    }

    @Override
    public List<FebackPo> searchList(FebackPo inpo) {
        List<FebackPo> outlist = null;
        try {
            outlist = decFeedbackMapper.searchList(inpo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return outlist;
    }

    @Override
    public FebackPo searchFedbackPo(FebackPo inpo) {
        FebackPo outlist = null;
        try {
            outlist = decFeedbackMapper.searchFedbackPo(inpo);
            List<String> ret = StringUtil.StrAsArray(outlist.getPicStrList());
            outlist.setFebackPiclist(ret);
            outlist.setPicStrList(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return outlist;
    }

    @Override
    public JsonResult delete(FebackPo inpo) {
        JsonResult result = new JsonResult();
        try {
            decFeedbackMapper.delete(inpo.getFebackid());
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(JsonResult.FAILED_CODE);
            result.setMsg("删除反馈信息失败！");
            return result;
        }
        result.setCode(JsonResult.SUCCESS_CODE);
        result.setMsg("删除反馈信息成功！");
        return result;
    }
}
